package com.unicom.netpd.rpa.global;

import com.unicom.netpd.rpa.constant.ResponseCodeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.io.Serializable;

/**
 * 保证序列化json的时候,如果是null的对象,key也会消失
 *
 * @author lxr
 * @param <T> data type
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class ServerResponse<T> implements Serializable {

    @ApiModelProperty(value = "状态码(200:正常，其他：异常)")
    private int status;
    @ApiModelProperty(value = "状态信息")
    private String msg;
    @ApiModelProperty(value = "响应数据")
    private T data;

    private ServerResponse(int status) {
        this.status = status;
    }

    private ServerResponse(int status, T data) {
        this.status = status;
        this.data = data;
    }

    private ServerResponse(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public static <T> ServerResponse<T> ok() {
        return new ServerResponse<T>(ResponseCodeEnum.SUCCESS.getCode());
    }

    public static <T> ServerResponse<T> createBySuccessMessage(String msg) {
        return new ServerResponse<T>(ResponseCodeEnum.SUCCESS.getCode(), msg);
    }

    public static <T> ServerResponse<T> ok(T data) {
        return new ServerResponse(ResponseCodeEnum.SUCCESS.getCode(), data);
    }

    public static <T> ServerResponse<T> ok(String msg, T data) {
        return new ServerResponse<T>(ResponseCodeEnum.SUCCESS.getCode(), msg, data);
    }


    public static <T> ServerResponse<T> error(String msg, T data) {
        return new ServerResponse<T>(ResponseCodeEnum.ERROR.getCode(), msg, data);
    }


    public static <T> ServerResponse<T> createByErrorMessage(String errorMessage) {
        return new ServerResponse<T>(ResponseCodeEnum.ERROR.getCode(), errorMessage);
    }

    public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode, String errorMessage) {
        return new ServerResponse<T>(errorCode, errorMessage);
    }

    public static ServerResponse error(int code, String msg) {
        return new ServerResponse(code, msg);
    }
    public static ServerResponse error(String msg) {
        return new ServerResponse(500, msg);
    }
}
